<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends Public_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('DX_Auth');
	}
	
	public function login()
	{
		$response = array();
		if( isset($_POST['username']) )
		{
			
			$ip = $this->adodb->GetAssoc("SELECT id,ip_address FROM sys_ip_auth WHERE active = 1");
			$ip_address = $this->input->ip_address();			
			
			if( !empty($ip) and !in_array($ip_address,$ip) )
			{
				$msg = error("Your IP Address is not allowed to login. Please contact Administrator");
				
				$response['status'] = 0;
				$response['msg'] = $msg;
				
				
				if( IS_AJAX == 1 ){
					$this->output->set_content_type('application/json')->set_output(json_encode($response));
					exit();	
				}else{
					set_msg( $msg );
					redirect("admin");
					exit();
				}
			}
			else
			{			
			
				$val = $this->form_validation;  
	
				$val->set_rules('username', 'Username', 'trim|required|xss_clean');  
				$val->set_rules('password', 'Password', 'trim|required|xss_clean');  
				$val->set_rules('remember', 'Remember me', 'integer');
				
				if( $val->run() == false )
				{
					$msg = validation_errors("<font color=\"red\">","</font><br>");
					
					if( IS_AJAX == 1 )
					{
						$response['status'] = 0;
						$response['msg'] = $msg;
					}
					else
					{					
						set_msg( $msg );
						redirect("admin");
						exit();
					}
				}else{			
				
					if( $this->dx_auth->is_banned() )
					{
						$msg = error("Username Anda telah dibanned. Silahkan kontak administrator");
						$this->set_log("Error login with message : $msg");
						if( IS_AJAX == 1 )
						{
							$response['status'] = 0;
							$response['msg'] = $msg;
						}
						else
						{
							set_msg( $msg );
							redirect("admin");
							exit();
						}
							
					}
					else
					{
						$username = $this->input->post('username',true);
						$password = $this->input->post('password',true);
						$remember = $this->input->post('remember');
						
						$login = $this->dx_auth->login($username, $password, $remember);
						if( $login )
						{
							$this->set_log("User ID $username Success Login");
							if( IS_AJAX == 1 )
							{
								$response['status'] = 1;
								$response['msg'] = success("Login berhasil. Mohon tunggu");
								$response['redirect'] = site_url('main');
							}
							else
							{
								redirect('main');
								exit();	
							}
						}
						else
						{
							$msg = "<font color=\"red\">Username atau password Anda tidak benar</font><br>";
							$this->set_log("User ID $username Failed login. MSG : $msg");
							if( IS_AJAX == 1 )
							{
								$response['status'] = 0;
								$response['msg'] = $msg;
							}
							else
							{
								
								set_msg( $msg );
								redirect("admin");
								exit();	
							}
						}
					}
				}
			
			}
			
			if( IS_AJAX == 1 ){
				$this->output->set_content_type('application/json')->set_output(json_encode($response));	
			}			  	
		}
		else
		{
			show_404();	
		}
	}
	
	public function logout()
	{
		if( $this->dx_auth->is_logged_in() )
		{			
			$userid = $this->session->userdata['DX_username'];
			$this->set_log("USER ID $userid LOGOUT");
			$this->dx_auth->logout();
		}
		redirect( _URL );
		exit();
	}
}